package cn.com.yusys.yusp.admin.service;

import cn.com.yusys.yusp.admin.domain.AdminSmLookup;
import cn.com.yusys.yusp.admin.domain.AdminSmLookupItem;
import cn.com.yusys.yusp.admin.domain.AdminSmLookupType;
import cn.com.yusys.yusp.admin.repository.mapper.AdminSmLookupItemMapper;
import cn.com.yusys.yusp.admin.repository.mapper.AdminSmLookupMapper;
import cn.com.yusys.yusp.admin.repository.mapper.AdminSmLookupTypeMapper;
import cn.com.yusys.yusp.commons.mapper.CommonMapper;
import cn.com.yusys.yusp.commons.mapper.QueryModel;
import cn.com.yusys.yusp.commons.service.CommonService;
import cn.com.yusys.yusp.commons.util.DateUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @项目名称：yusp-admin
 * @类名称：AdminSmLookupTypeService
 * @类描述：数据字典类别目录
 * @功能描述:
 * @创建人：liaoxd@yusys.com.cn @创建时间：2017-12-13 11:19 @修改备注：
 * @修改日期 修改人员 修改原因 -------- -------- ----------------------------------------
 * @version 1.0.0
 * @Copyright (c) 2017宇信科技-版权所有
 */
@Service
public class AdminSmLookupTypeService extends CommonService {
	@Autowired
	private AdminSmLookupTypeMapper mapper;
	@Autowired
	private AdminSmLookupMapper lookupMapper;
	@Autowired
	private AdminSmLookupItemMapper itemMapper;
	

	@Override
	protected CommonMapper<?> getMapper() {
		return this.mapper;
	}

	@Override
	public int insert(Object record) {
		AdminSmLookupType adminSmLookupType = (AdminSmLookupType) record;
		adminSmLookupType.setLastChgDt(DateUtil.formatDateTimeByDef());
		return this.insert(getMapper(), record);
	}
	@Override
	public int updateSelective(Object record) {
		AdminSmLookupType adminSmLookupType = (AdminSmLookupType) record;
		adminSmLookupType.setLastChgDt(DateUtil.formatDateTimeByDef());
		return this.updateSelective(getMapper(), record);
	}

	/**
     * @函数名称:selectByModel
     * @函数描述:重写，支撑页面模糊查询和用户名显示
     * @参数与返回说明:
     * @算法描述:
     */
	@Override
	public <T> List<T> selectByModel(QueryModel model) {
		if (model.getCondition().containsKey("lookupTypeName")) {
			model.getCondition().put("lookupTypeName", "%" + model.getCondition().get("lookupTypeName") + "%");
		}

		return this.selectByModel(getMapper(), model);
	}
	
	/**
     * @函数名称:
     * @函数描述:全量查询
     * @参数与返回说明:
     * @算法描述:
     */
	public List<AdminSmLookupType> getTypeList() {
		return this.mapper.selectLookupType();
	}
	
	/**
     * @函数名称:delete
     * @函数描述:级联删除，删除目录以及目录下的类别、类别下的字典内容
     * @参数与返回说明:
     * @算法描述:
     */
	@Transactional(readOnly = false, rollbackFor = {Exception.class, RuntimeException.class})
	public int deleteByTypeId(String id) {
		int result = this.deleteByPrimaryKey(id);
		Map<String, String> map = new HashMap<String, String>();
		map.put("lookupTypeId", id);
		
				
		AdminSmLookup inLookup = new AdminSmLookup();
		inLookup.setLookupTypeId(id);
		List<AdminSmLookup> list = this.lookupMapper.select(inLookup);
		if(list != null && list.size()>0) {
			for(int i=0;i<list.size();i++) {
				AdminSmLookup adminSmLookup = (AdminSmLookup) list.get(i);
				AdminSmLookupItem item = new AdminSmLookupItem();
				item.setLookupCode(adminSmLookup.getLookupCode());			
				this.itemMapper.delete(item);
			}
		}
		
		this.lookupMapper.deleteByTypeId(map);
		
		return result;
	}

}
